package cn.yza.线程.异步;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Supplier;

/** 多任务组合
 * @author Honglixi
 * @create 2021年11月7日 14:17:06
 */
public class Test4 {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<String> future01 = CompletableFuture.supplyAsync((new Supplier<String>() {
            @Override
            public String get() {
                System.out.println("查询商品的图片信息");
                return "Hello.png";
            }
            }), executor);
        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "属性";
        }, executor);

        CompletableFuture<String> future03 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品的介绍");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "介绍";
        }, executor);
        //多线程阻塞式等待
//        future01.get();
//        future02.get();
//        future03.get();
        //
        CompletableFuture<Void> allOf = CompletableFuture.allOf(future01, future02, future03);
        allOf.get();
//        allOf.join();
        System.out.println("main线程" + future01.get()+future02.get()+future03.get());
    }
}
